package com.shushan.pages.newtype.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shushan.pages.newtype.bean.NewWordBean;
import com.shushan.shushanbao.R;
import com.shushan.util.DensityUtil;

import java.util.List;

/**
 * @ClassName: StrangeWordsRecyclerAdapter
 * @Desciption: //生字显示recyclerView适配器
 * @author: zhangshihao
 * @date: 2018-06-21
 */
public class StrangeWordsRecyclerAdapter extends RecyclerView.Adapter<StrangeWordsRecyclerAdapter.ViewHolder>{

    private static final String TAG = StrangeWordsRecyclerAdapter.class.getSimpleName();

    private Context context;
    private List<NewWordBean> newWordBeanList;

    private OnItemClickListener onItemClickListener;

    public StrangeWordsRecyclerAdapter(){}

    public StrangeWordsRecyclerAdapter(Context context, List<NewWordBean> newWordBeanList){
        this.context = context;
        this.newWordBeanList = newWordBeanList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_strange_words,null,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        int screenWidth = DensityUtil.getScreenWidth(context);
        int boxWidth = (screenWidth - DensityUtil.dip2px(context,60))/5 - DensityUtil.dip2px(context,4f);
        holder.rlContainer.setLayoutParams(new RelativeLayout.LayoutParams(boxWidth,boxWidth));
        final NewWordBean newWordBean = newWordBeanList.get(position);
        holder.tvStrangeWord.setText(newWordBean.name);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null){
                    onItemClickListener.onItemClick(position,newWordBean.id);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return newWordBeanList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tvStrangeWord;
        private RelativeLayout rlContainer;
        public ViewHolder(View itemView) {
            super(itemView);
            tvStrangeWord = itemView.findViewById(R.id.tv_strange_word);
            rlContainer = itemView.findViewById(R.id.rl_container);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(int position,int newWordId);
    }

}
